package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * Project Name: reggie_take_out
 * File Name: SetmealController
 *
 * @version 1.0
 * @author:wenerduo
 * @Date: 2022/07/29/下午 12:00
 * Copyright (c) 2022,
 */
//套餐管理
    @RestController
    @RequestMapping("/setmeal")
    @Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;
    @Resource
    private CategoryService categoryService;
    //新增套餐
      @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
          log.info("套餐信息：SetmealDto===>{}",setmealDto);
          setmealService.saveWithDish(setmealDto);
           return  R.success("套餐信息插入成功");

    }
       //套餐分页查询
    @GetMapping("/page")
    public  R<Page>  page(int page,int pageSize,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto>dtoPage =new Page<>();
        // 构造条件
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        // 模糊查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件，根据 更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,queryWrapper);
        List<Setmeal>  records=pageInfo.getRecords();
        List<SetmealDto>list=records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
           // id查出分类名称

            Long categoryId=item.getCategoryId();
            Category category= categoryService.getById(categoryId);
            if (category!=null){
                String categoryName=category.getName();
                setmealDto.setCategiryName(categoryName);
            }
            return  setmealDto;

        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
    /**根据id删除套餐信息,同时删除关联的菜品
     * id
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        setmealService.removeWithDish(ids);

        return R.success("套餐数据删除成功");
    }


    /**
     * 套餐的起售与停售
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> onOrClose(@PathVariable Integer status,Long[] ids){
        log.info("setmel==>{},status==>{}"+ids+"===>" +status);
        for (int i=0;i<ids.length;i++){
            //获取菜品
            Setmeal setmeal=setmealService.getById(ids[i]);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }




}
